import java.io.Serializable;


/* Alice Robinson and Holly Beach
 * TCSS491 - Winter 2012*/

/**A PHouse is a PhysicsObject with a House Renderable object.
 * It has 4 constructors:  One takes the size,
 * One takes the size and pickable boolean
 * One takes the the size, red, green, and blue colors
 * and one takes the size, pickable boolean, red, green, and blue colors */
public class PHouse extends PhysicsObject implements Serializable {
	private static final long serialVersionUID = 5802919474721397631L;

	private static final float[] VERTICES = {0,0,
											1f,0,
											1f,1f,
											.5f, (float)Math.sqrt(.5),
											0,1f};
	
	private Vector2f[] vertexCache;
	private Vector2f[] normalCache;
	
	private class Renderable extends SceneGraphNode implements Serializable{
		private static final long serialVersionUID = 139725350515033115L;

		public Renderable(){
			this(true);
		}	
		public Renderable(boolean pickable) {
			super(pickable);				
			SceneGraphNode child = (new PSquare(1f, false, red, green, blue)).renderable;
			addChild(child);
			child = (new PTriangle(1f, false,red+.2f, green+.2f, blue+.2f)).renderable;
			child.translateX = .5f;
			child.translateY = 1.2f;
			addChild(child);			
		}
		public Renderable(boolean pickable, float red, float green, float blue) {
			super(pickable);				
			SceneGraphNode child = (new PSquare(1f, false, red, green, blue)).renderable;
			addChild(child);
			child = (new PTriangle(1f, false,red+.2f, green+.2f, blue+.2f)).renderable;
			child.translateX = .5f;
			child.translateY = 1.2f;
			addChild(child);			
		}
	}
	
	public PSquare base;
	public PTriangle roof;
	
	public PHouse(){
		base = new PSquare();
		roof = new PTriangle();
		renderable = new Renderable(true);
		renderable.scale = 1f;
		this.radius = 2*renderable.scale/3;
		
	}
	public PHouse(float size) {
		this.radius = 2* size/3;
		renderable = new Renderable(true);
		renderable.scale = size;
	}
	public PHouse(float size, float red,float green, float blue){
		this.radius = 2* size/3;
		renderable = new Renderable(true, red, green, blue);
		renderable.scale = size;
		renderable.red = red;
		renderable.green = green;
		renderable.blue = blue;
	}
	
	public void clearCaches() {
		vertexCache = null;
		normalCache = null;
	}

	public Vector2f[] getVertices() {	
		if (vertexCache == null) {
			vertexCache = new Vector2f[VERTICES.length / 2];
			Vector2f tmp = new Vector2f();
			for (int i = 0; i < VERTICES.length; i += 2) {
				tmp.x = VERTICES[i] * renderable.scale;
				tmp.y = VERTICES[i+1] * renderable.scale;
				tmp.sumScale(centerOfMass, -1);
				vertexCache[i/2] = new Vector2f();
				vertexCache[i/2].x = (float)(Math.cos(orientation) * tmp.x - Math.sin(orientation) * tmp.y);
				vertexCache[i/2].y = (float)(Math.sin(orientation) * tmp.x + Math.cos(orientation) * tmp.y);
				vertexCache[i/2].sum(centerOfMass);
				vertexCache[i/2].sum(position);
			}
		}
		return vertexCache;
	}
	
	public Vector2f[] getNormals() {
		if (normalCache == null) {
			Vector2f[] vertices = getVertices();
			normalCache = new Vector2f[vertices.length];

			for (int i = 0; i < vertices.length; i++) {
				normalCache[i] = new Vector2f(vertices[(i+1)%vertices.length]);
				normalCache[i].sumScale(vertices[i], -1);
				normalCache[i].normalize();
				float tmp = normalCache[i].x;
				normalCache[i].x = normalCache[i].y;
				normalCache[i].y = -tmp;
			}
		}
		return normalCache;
	}

}
